package com.bsoft.gol.hcs.clinical.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import hcn.base.drug.BaseCurrencyDrug;
import hcn.base.drug.qo.DrugQo;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public abstract class BaseCurrencyDrugDAO extends HibernateSupportDelegateDAO<BaseCurrencyDrug> {
    public BaseCurrencyDrugDAO(){
        this.setKeyField("drugInfoId");
    }

    @DAOMethod(sql=" from BaseCurrencyDrug where drugName=:drugName and dosageFormCode=:dosageFormCode")
    public abstract BaseCurrencyDrug getBaseCurrencyDrug(@DAOParam("drugName")String drugName, @DAOParam("dosageFormCode")String dosageFormCode);

    /**
     * 查询标准药品
     * @param drugQo
     * @return
     */
    public List<Map<String,Object>> findStandardDrugList(final DrugQo drugQo){
        HibernateStatelessResultAction<List<Map<String,Object>>> action = new AbstractHibernateStatelessResultAction<List<Map<String,Object>>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql  = new StringBuffer();
                sql.append("select a.* from (");
                sql.append(" SELECT b.dosage_form_name as dosageFormName,b.modify_dt,bf.factory_name as factoryName,");
                sql.append("b.prescription_flag as prescriptionFlag,b.drug_name AS drugName,b.pinyin_code AS pinyinCode,b.drug_type AS drugType,");
                sql.append("b.dosage_form_code AS dosageFormCode,bd.status,bd.drug_id AS drugId,bd.drug_info_id AS drugInfoId,");
                sql.append(" IF((select count(1) from base_drug_contrast bc where bd.drug_id =bc.drug_id )>0,1,0) as checkStatus,");
                sql.append("bd.specification,bf.short_name AS shortName,bd.pack_unit AS packUnit,bd.factory_id AS factoryId,bd.price ");
                sql.append("FROM base_drug bd");
                sql.append(" LEFT JOIN  base_currency_drug b on bd.drug_info_id = b.drug_info_id");
                sql.append(" LEFT JOIN base_factory bf on bf.factory_id = bd.factory_id");
                sql.append(") a where 1 = 1 ");
                if(CommonUtils.isNotEmpty(drugQo.getDrugType())){
                    sql.append(" and a.drugType = '"+drugQo.getDrugType()+"'");
                }

                if(CommonUtils.isNotEmpty(drugQo.getFactoryName())){
                    sql.append(" and a.shortName like '%"+drugQo.getFactoryName()+"%'");
                }

                if(CommonUtils.isNotEmpty(drugQo.getDrugName())){
                    sql.append(" and a.drugName like '%"+drugQo.getDrugName()+"%'");
                }

                if(CommonUtils.isNotEmpty(drugQo.getPinyinCode())){
                    sql.append(" and a.pinyinCode like '%"+drugQo.getPinyinCode()+"%'");
                }

                if(CommonUtils.isNotEmpty(drugQo.getStatus())){
                    sql.append(" and a.status = '"+drugQo.getStatus()+"'");
                }

                sql.append(" order by modify_dt desc");
                Query sqlQuery = ss.createSQLQuery(sql.toString());
                int first = drugQo.getPageNo() > 0 ? (drugQo.getPageNo() - 1) * drugQo.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(drugQo.getPageSize());
                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map<String,Object>> dbList = sqlQuery.list();
                setResult(dbList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }



    /**
     * 查询标准
     * @param drugQo
     * @return
     */
    public BigInteger getStandardDrugCount(final DrugQo drugQo){
        HibernateStatelessResultAction<BigInteger> action = new AbstractHibernateStatelessResultAction<BigInteger>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql  = new StringBuffer();
                sql.append("select count(0) from (");
                sql.append(" SELECT b.drug_name AS drugName,b.pinyin_code AS pinyinCode,b.drug_type AS drugType,b.dosage_form_code AS dosageFormCode,b.status,bd.drug_id AS drugId,bd.drug_info_id AS drugInfoId,bd.specification,bf.short_name AS shortName,bd.pack_unit AS packUnit,bd.factory_id AS factoryId,bd.price FROM base_drug bd ");
                sql.append(" LEFT JOIN  base_currency_drug b on bd.drug_info_id = b.drug_info_id");
                sql.append(" LEFT JOIN base_factory bf on bf.factory_id = bd.factory_id");
                sql.append(") a where 1 = 1");
                if(CommonUtils.isNotEmpty(drugQo.getDrugType())){
                    sql.append(" and a.drugType = '"+drugQo.getDrugType()+"'");
                }

                if(CommonUtils.isNotEmpty(drugQo.getFactoryName())){
                    sql.append(" and a.shortName like '%"+drugQo.getFactoryName()+"%'");
                }

                if(CommonUtils.isNotEmpty(drugQo.getDrugName())){
                    sql.append(" and a.drugName like '%"+drugQo.getDrugName()+"%'");
                }

                if(CommonUtils.isNotEmpty(drugQo.getPinyinCode())){
                    sql.append(" and a.pinyinCode like '%"+drugQo.getPinyinCode()+"%'");
                }

                if(CommonUtils.isNotEmpty(drugQo.getStatus())){
                    sql.append(" and a.status = '"+drugQo.getStatus()+"'");
                }
                Query sqlQuery = ss.createSQLQuery(sql.toString());
                List list = sqlQuery.list();
                if (list.size() == 1) {
                    Object o = list.get(0);
                    BigInteger count = (BigInteger) o;
                    setResult(count);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
